/config,nproc,4
/config,nres,20000
/FILNAME, FSW_Al6061_conta
/PREP7
/title, sequentially coupled analysis with heat flux on top surface
Antype,trans

ET,1,SOLID70
MP,DENS,1,2700
MP,KXX,1,167
MPTEMP,,0,40,125,225,325,425,575,645
MPDATA,C,1,,910,950,1000,1050,1100,1150,1270,1310


length=0.24
tlength=0.18
width=0.05
thick=0.006
xstart=0.03
nthke=4
smesize=0.0015

ncopy=NINT(length/(4*smesize))
block,,4*smesize,,thick,,10*smesize
block,,4*smesize,,thick,10*smesize,12*smesize
block,,4*smesize,,thick,12*smesize,14*smesize
block,,4*smesize,,thick,14*smesize,width
nummrg,all
numcmp,all
lesize,1,,,nthke
MSHAPE,0,3D
MSHKEY,1
esize,smesize
vMESH,1
lesize,15,,,2,
lesize,13,,,2,
lesize,14,,,4,
lesize,16,,,4,
lesize,17,,,1,
lesize,20,,,1,
lesize,18,,,1,
lesize,19,,,1,
vMESH,2
lesize,22,,,2,
lesize,24,,,2,
lesize,21,,,2,
lesize,23,,,2,
lesize,25,,,1,
lesize,26,,,1,
lesize,27,,,1,
lesize,28,,,1,
vmesh,3
lesize,36,,,9
vmesh,4
VGEN,ncopy, all,,,4*smesize,,,,,
VSYMM, Z, all
nummrg,all
FINISH

/SOLU 
tunif,25 
allsel
nsel,s,loc,y,
SF,all,CONV,1000,20 
allsel
nsel,s,loc,x,0
nsel,a,loc,x,length
nsel,a,loc,z,width
nsel,a,loc,y,thick
SF,all,CONV,15,20
allsel


kbc,1 
time,0.001
DELTIM,0.001 
solve

AUTOTS,ON
ddept=0.006
ddtime=0.2
dvel=0.0002
dinc=ddtime*dvel
dtime=0.1
dtotal=10
dweltime=2
ndwel=dweltime/dtime
dnloct=dtotal/ddtime 
tvel=0.00233                      !welding speed
xinc=dtime*tvel
nloct=NINT(tlength/xinc)
rshoulder=0.012
rprobe=0.003
phinput=220
nheatgen=phinput/(dnloct*dinc*3.14*rprobe*rprobe)
ehfulx=220*8.7

    nsel,s,loc,y,thick
    nsel,r,loc,z,-rprobe,rprobe
    nsel,r,loc,x,-rprobe,rprobe
    ESLN,s,
    SFEDELE,all,4,CONV
allsel
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)
    *get,numnode,node,0,count   !number of nodes in the selected set
    *get,lnnum,node,0,num,min    !loest node number in the selected set
    *dim,nodenumber,ARRAY,numnode 
    *dim,nodenumber2,ARRAY,numnode 
    *dim,nodenumber3,ARRAY,numnode 
    *dim,nodehflux,ARRAY,numnode
    *dim,emnumber,ARRAY,numnode 
    allsel
! plunge
*do,ka,1,dnloct,1
    x0=xstart   !start point
    z0=0
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)

     nnum=lnnum
     kd=0
    *do,kb,1,numnode
       *get,xlocation,node,nnum,loc,x
       *get,zlocation,node,nnum,loc,z
       *get,ylocation,node,nnum,loc,y
        rd=SQRT((xlocation-x0)**2+(zlocation-z0)**2)

          *if,rd,le,rprobe,THEN
            *if,ylocation,ge,(thick-ka*dinc),then
	            kd=kd+1
	            nodenumber2(kd)=nnum
 	            bf,nnum,HGEN,nheatgen
             *endif
          *endif
       *get,nexthn,node,nnum,nxth
        nnum=nexthn
     *enddo
    allsel
        step2time=ka*ddtime+0.001
        time,step2time
        DELTIM,ddtime
        KBC,1 
        outres,BASIC,LAST       
        solve
/solu
    *do,ke,1,kd
     bfdele,nodenumber2(ke),HGEN
    *enddo
*enddo
allsel


*do,i,1,(nloct+ndwel),1
        *if,i,le,ndwel,then
           x0=xstart
        *else
          x0=xstart+(i-ndwel)*xinc   !start point
        *endif
           z0=0
    nsel,s,loc,z,-(rshoulder+smesize),(rshoulder+smesize)

     nnum=lnnum
     jj=0
     kk=0
     tt=0
    *do,ia,1,numnode
       *get,xlocation,node,nnum,loc,x
       *get,zlocation,node,nnum,loc,z
       *get,ylocation,node,nnum,loc,y
        rd=SQRT((xlocation-x0)**2+(zlocation-z0)**2)

          *if,rd,le,rshoulder,and,rd,gt,rprobe,THEN
            *if,ylocation,eq,thick,then
            jj=jj+1
            nodenumber(jj)=nnum
            nodehflux(jj)=3*ehfulx*rd/(2*3.1416*(rshoulder**3-rprobe**3))
            *endif
          *endif
          *if,rd,le,rshoulder,THEN
            *if,ylocation,eq,thick,then
            tt=tt+1
            nodenumber3(tt)=nnum
            *endif
          *endif
          *if,rd,le,rprobe,then
            kk=kk+1
            nodenumber2(kk)=nnum
             bf,nnum,HGEN,nheatgen
          *endif
       *get,nexthn,node,nnum,nxth
        nnum=nexthn
     *enddo
    allsel
     *do,ta,1,tt,1
        *if,ta,eq,1,then
          nsel,s,node,,nodenumber3(ta) 
        *else
         nsel,a,node,,nodenumber3(ta)
        *endif
     *enddo
     ESLN,s,
     SFEDELE,all,4,CONV
    allsel

     *do,ib,1,jj,1
        *if,ib,eq,1,then
          nsel,s,node,,nodenumber(ib) 
        *else
         nsel,a,node,,nodenumber(ib)
         n3=nodenumber(ib)
        *endif
     *enddo
     ESLN,s,

     *get,numelem,elem,0,count
     *get,lenum,elem,0,num,min

     enum=lenum
     nsle,all    
    *do,ii,1,numelem
     emnumber(ii)=enum
     nnumk=nelem(enum,3) 
     nnuml=nelem(enum,4) 
     nnumo=nelem(enum,7) 
     nnump=nelem(enum,8) 
     hfluxl=0
     hfluxk=0
     hfluxp=0
     hfluxo=0
       *do,iii,1,jj
          *if,nnuml,eq,nodenumber(iii),then
              hfluxl=nodehflux(iii)
             *elseif,nnumk,eq,nodenumber(iii)
              hfluxk=nodehflux(iii)
             *elseif,nnump,eq,nodenumber(iii)
              hfluxp=nodehflux(iii)
             *elseif,nnumo,eq,nodenumber(iii)
              hfluxo=nodehflux(iii)
          *endif
       *enddo
       aehflux=(hfluxl+hfluxk+hfluxp+hfluxo)/4
       sfe,enum,4,HFLUX,,hfluxk,hfluxl,hfluxp,hfluxo 

      *get,nexthe,elem,enum,nxth
      enum=nexthe
   *enddo
  allsel
  step3time=step2time+i*dtime          
        time,step3time
        DELTIM,dtime
        KBC,1 
        outres,erase
        outres,BASIC,LAST        
        solve
/solu
    *do,id,1,numelem
     enum=emnumber(id)
     SFEDELE,enum,4,hflux
    *enddo
    *do,ik,1,kk
     bfdele,nodenumber2(ik),HGEN
    *enddo
    nsel,s,loc,y,thick
    SF,all,CONV,15,20
    allsel
*enddo

*do,hhkk,1,60
step5time=hhkk*0.2+step3time
        time,step5time
        DELTIM,0.2
	outres,erase
        outres,BASIC,LAST 
        solve
*enddo
step5time=step5time+0.2
        time,step5time
        DELTIM,0.2
	outres,erase
        outres,BASIC,LAST 
        solve
finish
/PREP7
Antype,trans
NROPT,UNSYM
ET,1,SOLID45
mp,ex,1, 200e9        
mp,alpx,1,0.000025
mp,nuxy,1,.33
MP,DENS,1,2700
!   
TB,BKIN,1,5               ! Activate a data table
TBTEMP,0                ! Temperature = 25
TBDATA,1,280E6,0           ! Yield = 44,000; Tangent modulus = 0
TBTEMP,125                
TBDATA,1,270E6,0
TBTEMP,225                
TBDATA,1,220E6,0 
! TBTEMP,500                
! TBDATA,1,200E6,0
! TBTEMP,600                
! TBDATA,1,110E6,0
! TBTEMP,650                
! TBDATA,1,75E6,0 
! TBTEMP,700                
! TBDATA,1,50E6,0
TBTEMP,325                
TBDATA,1,70E6,0
TBTEMP,425                
TBDATA,1,20E6,0   
!   
nsel,all
nsel,s,loc,z,width
nsel,a,loc,z,-width
nsel,r,loc,y,0
d,all,all,0
nsel,all
WPOFFS, xstart, thick
WPROTA,,-90
CYLIND,,rshoulder,,4*thick,0,360, 
WPROTA,,90
WPOFFS, -xstart, -thick
MP,MU,1,0.3
MAT,1   
R,3 
REAL,3  
ET,2,170
ET,3,174
KEYOPT,3,9,0
KEYOPT,3,10,2   
R,3,
RMORE,  
RMORE,,0
RMORE,0 
! Generate the target surface   
asel,s,loc,y,thick
asel,r,loc,z,-4*smesize,4*smesize
CM,_TARGET,AREA 
AATT,-1,3,2,-1  
TYPE,2  
AMESH,ALL   
allsel
! Create a pilot node   
N,40000, xstart,thick,0  
TSHAP,PILO  
E,40000
! Generate the contact surface  
asel,s,loc,y,thick
asel,r,loc,z,-10*smesize,10*smesize
asel,u,,,26
CM,_CONTACT,AREA
TYPE,3  
NSLA,S,1
ESLN,S,0
ESURF   
ALLSEL 
block,-4*smesize,length+4*smesize,-smesize,,-width-4*smesize,width+4*smesize
/COM, CONTACT PAIR CREATION - START 
MP,MU,1,0.3 
MAT,1   
R,4 
REAL,4  
ET,4,170
ET,5,174
KEYOPT,5,9,0
KEYOPT,5,10,2   
R,4,
! Generate the target surface   
ASEL,S,,,62
CM,_TARGET,AREA 
AATT,-1,4,4,-1  
TYPE,4
esize,0.006  
AMESH,ALL   
! Create a pilot node   
N,80000, 0,0,0  
TSHAP,PILO  
E,80000
! Generate the contact surface  
allsel
ASEL,S,loc,y,0
ASEL,U,,,62
CM,_CONTACT,AREA
TYPE,5  
NSLA,S,1
ESLN,S,0
ESURF   
*SET,_REALID,4  
ALLSEL  
ESEL,ALL
/PNUM,TYPE,1
/NUM,1  
EPLOT   
/COM, CONTACT PAIR CREATION - END   

finish
/solu
D,40000,UZ,0
D,40000,UX,0
D,40000,ROTX,0
D,40000,ROTZ,0
d,80000,all,0
NLGEOM,off
AUTOTS,off
tref,25
ldread,temp,1,,,,,rth  ! Read in temperatures from thermal run
time,0.001
outres,erase
OUTRES,basic,last   
solve
!                        plunging stage
rsteps=dnloct+1
*do,iijj,2,rsteps
           *if,iijj,eq,rsteps,then
F,40000,FY,-10000,
           *else
F,40000,FY,-(10000/30)*(iijj-1)*ddtime,
           *endif 
ldread,temp,iijj,,,,,rth  ! Read in temperatures from thermal run
step2time=(iijj-1)*ddtime+0.001
time,step2time
DELTIM,ddtime/20,ddtime/10000,ddtime
outres,erase
OUTRES,basic,last    
kbc,0       
solve
*enddo

rsteps=rsteps+nloct+ndwel
*do,iikk,1,(nloct+ndwel-60),1
        *if,iikk,le,ndwel,then
		D,40000,UX,0
        *else
                D,40000,UX,(iikk-ndwel)*xinc  
        *endif
     F,40000,FY,-10000,,
ldread,temp,(iikk+iijj),,,,,rth  ! Read in temperatures from thermal run
step3time=iikk*dtime+step2time
time, step3time
DELTIM,dtime/20,dtime/10000,dtime
outres,erase
OUTRES,basic,last   
kbc,0     
solve
*enddo
readstep=iikk+iijj
*do,ikk,1,60,1
     D,7000,UX,(iikk+ikk-ndwel)*xinc  
     F,7000,FY,-10000,,
ldread,temp,(readstep+ikk),,,,,rth  ! Read in temperatures from thermal run
steptime=ikk*dtime+step3time
time, steptime
DELTIM,dtime/40,dtime/10000,dtime
outres,erase
OUTRES,basic,last   
kbc,0     
solve
*enddo

F,40000,FY,0 
D,40000,UX,nloct*xinc 
D,40000,UY,10*xinc,
D,80000,UY,-xinc
rsteps=readstep+ikk
*do,hhii,1,60   
*SET,step5time,0.2*hhii+steptime
*SET,kh,rsteps+hhii 
ldread,temp,kh,,,,,rth  ! Read in temperatures from thermal run 
time,step5time  
DELTIM,0.004,0.001,5 
outres,erase
OUTRES,BASIC,last   
kbc,0   
solve   
/solu   
*enddo
save
nsel,all
nsel,s,loc,z,width
nsel,a,loc,z,-width
nsel,r,loc,y,0
DDELE,all,all
allsel
nsel,s,loc,x,0
nsel,r,loc,y,thick
nsel,r,loc,z,width
d,all,all,0
allsel
*SET,step5time,0.2+step5time
ldread,temp,(kh+1),,,,,rth  ! Read in temperatures from thermal run 
time,step5time  
DELTIM,0.002,0.001,5 
outres,erase
OUTRES,BASIC,last   
kbc,0   
solve

finish
save